package cn.dankal.flyaudio.presenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import api.MyCardServiceFactory;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CouponEntity;
import cn.dankal.basiclib.pojo.req.ExchangeReq;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.contract.MyCardContract;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 12/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.presenter
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class MyCardPresenter implements MyCardContract.Presenter {
    private MyCardContract.View mView;
    private String state;
    @Override
    public void getDataList(int page, String state) {
        mView.showLoadingDialog();
        Map<String, Object> queryMap = new HashMap<>();
        if(StringUtil.isEmpty(state)){
            queryMap.put("state", this.state);
        }else{
            queryMap.put("state", state);
            this.state= state;
        }

        queryMap.put("page", page);
        queryMap.put("size", "10");
        MyCardServiceFactory.cardList(queryMap)
                .subscribe(new AbstractSubscriber<BaseResponse<List<CouponEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<List<CouponEntity>> baseListResponseBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.showProductList(baseListResponseBaseResponse.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
//                        List<CouponEntity> list = new ArrayList<>();
//                        list.add(new CouponEntity(
//                                "11",
//                                "300元抵用卷",
//                                "2017-09-08 11:11:00",
//                                "2017-09-08 11:11:00",
//                                state,
//                                "2017-09-08 11:11:00",
//                                "true"
//                        ));
                        mView.onError();
                    }
                });
    }

    @Override
    public void getExchange(String code) {
        mView.showLoadingDialog();
//        ExchangeReq exchangeReq = new ExchangeReq();
//        exchangeReq.setCode(code);
        MyCardServiceFactory.couponsExchange(code)
                .subscribe(new AbstractSubscriber<BaseResponse<CouponEntity>>(mView) {
                    @Override
                    public void onNext(BaseResponse<CouponEntity> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.onExchangeSuccess(objectBaseResponse.getData().getUuid());
//                        ToastUtils.showShort("兑换成功");
                    }

                    @Override
                    public void onError(Throwable e) {
//                        super.onError(e);
                        mView.dismissLoadingDialog();
                        mView.onExchangeError();
//                        ToastUtils.showShort("兑换失败");
                    }
                });
    }

    @Override
    public void attachView(@NonNull MyCardContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }
}
